import request from '@/utils/request'

// 冲突检测任务接口
export interface ConflictDetectionTask {
  id: number
  taskName: string
  keywords: string
  scope: string
  status: 'pending' | 'running' | 'completed' | 'failed' | 'stopped'
  conflictCount: number
  creator: string
  createTime: string
  completeTime?: string
  progress?: number
}

// 获取冲突检测列表请求参数
export interface GetConflictDetectionListParams {
  page: number
  page_size: number
}

// 获取冲突检测列表响应
export interface GetConflictDetectionListResponse {
  code: number
  message: string
  data: {
    items: ConflictDetectionTask[]
    total: number
    page: number
    page_size: number
  }
}

// 获取冲突检测列表
export const getConflictDetectionList = (params: GetConflictDetectionListParams) => {
  return request<{
    items: ConflictDetectionTask[]
    total: number
    page: number
    page_size: number
  }>({
    url: '/api/bgtask/list_task',
    method: 'POST',
    data: params
  })
}

// 创建冲突检测任务
export interface CreateConflictDetectionParams {
  taskName: string
  keywords: string[]
  scope: string[]
  description?: string
}

export const createConflictDetection = (params: CreateConflictDetectionParams) => {
  return request({
    url: '/api/bgtask/create_task',
    method: 'POST',
    data: params
  })
}

// 新增冲突检测任务
export interface AddCheckTaskParams {
  tid: string[]
  file_category: string
}

export const addCheckTask = (params: AddCheckTaskParams) => {
  return request({
    url: '/api/vkdb/add_check_task',
    method: 'POST',
    data: params
  })
}

// 停止冲突检测任务
export const stopConflictDetection = (taskId: number) => {
  return request({
    url: `/api/bgtask/stop_task/${taskId}`,
    method: 'POST'
  })
}

// 删除冲突检测任务
export const deleteConflictDetection = (taskId: number) => {
  return request({
    url: `/api/bgtask/delete_task/${taskId}`,
    method: 'DELETE'
  })
}

// 获取冲突检测详情 - 旧接口
export interface ConflictDetectionDetail {
  id: number
  taskName: string
  keywords: string
  scope: string
  status: string
  conflictCount: number
  creator: string
  createTime: string
  completeTime?: string
  conflicts: ConflictDetail[]
}

export interface ConflictDetail {
  id: number
  level: 'high' | 'medium' | 'low'
  type: string
  description: string
  documents: string[]
  analysisContent: string
  solutionContent: string
}

export const getConflictDetectionDetail = (taskId: number) => {
  return request<{
    code: number
    message: string
    data: ConflictDetectionDetail
  }>({
    url: `/api/bgtask/detail/${taskId}`,
    method: 'GET'
  })
}

// 获取检测详情 - 新接口
export interface CheckDetailParams {
  task_id: string
}

export interface CheckDetailResponse {
  code: number
  message: string
  data: CheckConflictDetail[]
}

export interface CheckConflictDetail {
  id: string
  create_time: string
  user_id: string
  key_words: string
  scope: string | null
  collide_file_name: string
  current_type: string
  collide_file_id: string
  collide_chapter: string
  current_chapter: string
  collide_content: string
  current_content: string
  download_url: string | null
  download_count: number
  topic: string
  phrase_a: string
  phrase_b: string
  description: string
  doc_id: string
  task_id: string
}

export const getCheckDetail = (taskId: string) => {
  return request<CheckDetailResponse>({
    url: `/api/vkdb/check_details?task_id=${taskId}`,
    method: 'POST'
  })
}

// 导出冲突检测报告
export const exportConflictDetectionReport = (taskId: number) => {
  return request({
    url: `/api/bgtask/export_report/${taskId}`,
    method: 'GET',
    responseType: 'blob'
  })
}
